package com.study.controller;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.study.model.pojo.Good;
import com.study.services.GoodsService;

/**文件上传的控制器
 * @author SMILE
 */
@Controller
public class FileUploadController {
	@Autowired
	GoodsService ser;
	
	@GetMapping("upload")
	public ModelAndView index(HttpServletRequest request){
	
		System.out.println("/uploaddddd");
		ModelAndView mv = new ModelAndView("/upload");
		return mv;
	}
	
	
	/**
	 * 图片上传
	 * @param file
	 * @param gid
	 * @param request
	 * @return
	 */
	@PostMapping("/upload")
	@ResponseBody
	public String upload(MultipartFile file,Integer gid,
					   HttpServletRequest request){
		
		System.out.println("======商品id:"+gid);
		HttpSession session = request.getSession();
		//file存储了上传的文件的所有信息
		try {
			System.out.println(file);
			//转存到指定目录
			//1、获取当前工程发布所在的物理路径，文件上传目录WebContent/upload
			ServletContext context = request.getServletContext();
			System.out.println("context:"+context);
			//2、全球唯一码作为新的文件名
			String newname = UUID.randomUUID().toString().toUpperCase();
			//3、获取源文件的后缀名（png）
			String suffix = "."+FilenameUtils.getExtension(file.getOriginalFilename());
			//4、创建新文件（存储在硬盘上，使用物理路径。）
			File new_file=new File("G:\\git\\CRM\\src\\main\\resources\\static\\gimg"+"/"+newname+suffix);
			//File new_file=new File("D:\\Users\\Administrator\\git\\CRM\\src\\main\\resources\\static\\gimg"+"/"+newname+suffix);
			//File new_file=new File("D:\\Users\\Administrator\\git\\CRM\\src\\main\\resources\\static\\gimg"+"/"+newname+suffix);
			System.out.println("new_file========:"+new_file);
			String good_img = "/gimg/"+newname+suffix;
			session.setAttribute("gooImg", good_img);
			if (gid != null) {
				ser.findGood(gid, good_img);
			}
			System.out.println("拼接的图片路径======："+good_img);

			session.setAttribute("gooImg", good_img);
			if (gid != null) {
				ser.findGood(gid, good_img);
			}

			//5、将源文件转存到新文件上
			file.transferTo(new_file);
			//ModelAndView mv = new ModelAndView("/goods/productList");
			return "/redirect:/glist";

		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	
}
